package com.youshang520i.thread2;/**
 *
 * @author youshang
 * @date 2021/03/29 10:00
 **/
public class ThreadTest1 {
    public static void main(String[] args) {
        ThreadTest1 threadTest1 = new ThreadTest1();
        Runnable runnable = () -> {
            while (true){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                threadTest1.hello();
            }
        };
        Thread thread = new Thread(runnable," thread - Hello");

        Runnable runnable2 = () -> {
            while (true){
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                threadTest1.world();
            }
        };
        Thread thread2 = new Thread(runnable2," thread - world");

        thread.start();
        thread2.start();

    }

    private Object lock1 = new Object();
    private Object lock2 = new Object();

    public void hello(){
        synchronized (lock1){
            synchronized (lock2){
                System.out.println(" hello ");
            }
        }
    }
    public void world(){
        synchronized (lock2){
            synchronized (lock1){
                System.out.println(" world ");
            }
        }
    }

}
